Brackets []
The typical JavaScript notation for array creation is brackets. They represent an ordered set of values with non-negative integers providing an index.
Syntax:
let properArray = ['apple', 'banana', 'orange'];
Example: To demonstrate creating a JavaScript array using the brackets.
let properArray = ['apple', 'banana', 'orange'];
// Accessing elements
console.log(properArray[0]); // Output: 'apple'
console.log(properArray[1]); // Output: 'banana'
console.log(properArray.length); // Output: 3
Output
apple banana 3
JavaScript Braces vs Brackets
In JavaScript, arrays are a fundamental data structure used to store ordered collections of elements. While both braces and brackets are used with arrays, they serve distinct purposes.
This article clarifies the difference between these notations and their proper usage.
Table of Content
- Braces {}
- Brackets []